﻿#include "widget.h"
#include "./ui_widget.h"
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QFormLayout>
#include<QGridLayout>
#include"GlobalHooks.h"
#include"Lua.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    guns<<QStringLiteral("无")<<"AK47"<<"LR300"<<"MP5A4"<<"THOMPSON"<<"SMG"<<"HMLMG"<<"M249"<<"SAR"<<"M39"<<"P250"<<"M92"<<"PROTOTYPE17"<<"PYTHON"<<"REVOLVER";
    setWindowTitle(QStringLiteral("腐蚀Rust压枪辅助"));

    sl=new QStackedLayout(this);

    setting_w=new QWidget(this);
    lb_1=new QLabel("1",setting_w);
    lb_2=new QLabel("2",setting_w);
    lb_3=new QLabel("3",setting_w);
    lb_4=new QLabel("4",setting_w);
    lb_5=new QLabel("5",setting_w);
    lb_6=new QLabel("6",setting_w);

    cbb_1=new QComboBox(setting_w);
    cbb_1->addItems(guns);
    cbb_2=new QComboBox(setting_w);
    cbb_2->addItems(guns);
    cbb_3=new QComboBox(setting_w);
    cbb_3->addItems(guns);
    cbb_4=new QComboBox(setting_w);
    cbb_4->addItems(guns);
    cbb_5=new QComboBox(setting_w);
    cbb_5->addItems(guns);
    cbb_6=new QComboBox(setting_w);
    cbb_6->addItems(guns);

    entergun=new QPushButton(QStringLiteral("修改枪械"),setting_w);

    lb_sens=new QLabel("SENSITIVITY",setting_w);
    lb_ads_sens=new QLabel("ADS_SENSITIVITY",setting_w);
    lb_fov=new QLabel("FOV",setting_w);

    lb_before=new QLabel(QStringLiteral("脚本内容"),setting_w);
    lb_before_sens=new QLabel(setting_w);
    lb_before_ads_sens=new QLabel(setting_w);
    lb_before_fov=new QLabel(setting_w);

    lb_after=new QLabel(QStringLiteral("修改为"),setting_w);
    le_sens=new QLineEdit(setting_w);
    QDoubleValidator *dv=new QDoubleValidator(0.0,1.0,3,setting_w);
    le_sens->setValidator(dv);
    le_ads_sens=new QLineEdit(setting_w);
    le_ads_sens->setValidator(dv);
    le_fov=new QLineEdit(setting_w);
    QIntValidator *iv=new QIntValidator(70,90,setting_w);
    le_fov->setValidator(iv);
    enterchange=new QPushButton(QStringLiteral("修改"),setting_w);
    start=new QPushButton(QStringLiteral("开始辅助"),setting_w);

    start_w=new QWidget(this);
    back=new QPushButton(QStringLiteral("返回"),start_w);
    gun=new QLabel(QStringLiteral("枪械"),start_w);
    scope=new QLabel(QStringLiteral("配镜"),start_w);
    name=new QLabel(QStringLiteral("名称"),start_w);
    gunname=new QLabel(start_w);
    scopename=new QLabel(start_w);
    pic=new QLabel(QStringLiteral("图片"),start_w);
    gunpic=new QLabel(start_w);
    scopepic=new QLabel(start_w);

    layout();
    luathread=new QThread(this);
    Lua *lua=Lua::getinstance();
    lua->run();
    lua->moveToThread(luathread);
    luathread->start();

    connect(this,&Widget::getsetting,lua,&Lua::getsetting,Qt::QueuedConnection);
    connect(lua,&Lua::send_sens,this,&Widget::setting_sens,Qt::DirectConnection);
    connect(lua,&Lua::send_ads_sens,this,&Widget::setting_ads_sens,Qt::DirectConnection);
    connect(lua,&Lua::send_fov,this,&Widget::setting_fov,Qt::DirectConnection);

    connect(this,&Widget::getguns,lua,&Lua::getguns,Qt::QueuedConnection);
    connect(lua,&Lua::send_cbb_1,this,&Widget::set_cbb1,Qt::DirectConnection);
    connect(lua,&Lua::send_cbb_2,this,&Widget::set_cbb2,Qt::DirectConnection);
    connect(lua,&Lua::send_cbb_3,this,&Widget::set_cbb3,Qt::DirectConnection);
    connect(lua,&Lua::send_cbb_4,this,&Widget::set_cbb4,Qt::DirectConnection);
    connect(lua,&Lua::send_cbb_5,this,&Widget::set_cbb5,Qt::DirectConnection);
    connect(lua,&Lua::send_cbb_6,this,&Widget::set_cbb6,Qt::DirectConnection);

    connect(enterchange,&QPushButton::clicked,this,&Widget::setsetting,Qt::DirectConnection);
    connect(this,&Widget::set_sens,lua,&Lua::set_sens,Qt::DirectConnection);
    connect(this,&Widget::set_ads_sens,lua,&Lua::set_ads_sens,Qt::DirectConnection);
    connect(this,&Widget::set_fov,lua,&Lua::set_fov,Qt::DirectConnection);

    connect(entergun,&QPushButton::clicked,this,&Widget::setguns,Qt::DirectConnection);
    connect(this,&Widget::setcbb,lua,&Lua::setcbb,Qt::DirectConnection);

    connect(this,&Widget::getguninfo,lua,&Lua::getguninfo,Qt::QueuedConnection);
    connect(lua,&Lua::sendgun,this,&Widget::setgun,Qt::DirectConnection);
    connect(this,&Widget::getscopeinfo,lua,&Lua::getscopeinfo,Qt::QueuedConnection);
    connect(lua,&Lua::sendscope,this,&Widget::setscope,Qt::DirectConnection);

    GlobalHooks* hook=GlobalHooks::getinstance();
    connect(hook,&GlobalHooks::keyPressed,lua,&Lua::changegun,Qt::DirectConnection);
    connect(hook,&GlobalHooks::mouseLeftButtonPressed,lua,&Lua::OnEvent,Qt::QueuedConnection);
    connect(hook,&GlobalHooks::mouseRightButtonPressed,lua,&Lua::OnEvent,Qt::QueuedConnection);


    connect(start,&QPushButton::clicked,[=](){
        sl->setCurrentWidget(start_w);
        emit getguninfo();
    });

    connect(back,&QPushButton::clicked,[=](){
        sl->setCurrentWidget(setting_w);
    });

    emit getsetting();
    emit getguns();
}

Widget::~Widget()
{
    delete ui;
    luathread->quit();
    luathread->wait();
}

void Widget::layout()
{
    QFormLayout* fl=new QFormLayout;
    fl->addRow(lb_1,cbb_1);
    fl->addRow(lb_2,cbb_2);
    fl->addRow(lb_3,cbb_3);
    fl->addRow(lb_4,cbb_4);
    fl->addRow(lb_5,cbb_5);
    fl->addRow(lb_6,cbb_6);
    fl->addRow(entergun);

    QGridLayout *gl=new QGridLayout;
    gl->addWidget(lb_sens,0,1);
    gl->addWidget(lb_ads_sens,0,2);
    gl->addWidget(lb_fov,0,3);

    gl->addWidget(lb_before,1,0);
    gl->addWidget(lb_before_sens,1,1);
    gl->addWidget(lb_before_ads_sens,1,2);
    gl->addWidget(lb_before_fov,1,3);

    gl->addWidget(lb_after,2,0);
    gl->addWidget(le_sens,2,1);
    gl->addWidget(le_ads_sens,2,2);
    gl->addWidget(le_fov,2,3);

    QVBoxLayout *vl=new QVBoxLayout;
    vl->addLayout(gl);
    vl->addWidget(enterchange);

    QHBoxLayout *hl2=new QHBoxLayout;
    hl2->addLayout(fl);
    hl2->addLayout(vl);

    QVBoxLayout *vl2=new QVBoxLayout;
    vl2->addLayout(hl2);
    vl2->addWidget(start);
    setting_w->setLayout(vl2);

    QGridLayout *gl2=new QGridLayout;
    gl2->addWidget(back,0,0);
    gl2->addWidget(gun,0,1);
    gl2->addWidget(scope,0,2);
    gl2->addWidget(name,1,0);
    gl2->addWidget(gunname,1,1);
    gl2->addWidget(scopename,1,2);
    gl2->addWidget(pic,2,0);
    gl2->addWidget(gunpic,2,1);
    gl2->addWidget(scopepic,2,2);

    start_w->setLayout(gl2);

    sl->addWidget(setting_w);
    sl->addWidget(start_w);
    sl->setCurrentWidget(setting_w);
}

void Widget::setting_sens(float sens)
{
    lb_before_sens->setNum(sens);
}

void Widget::setting_ads_sens(float ads_sens)
{
    lb_before_ads_sens->setNum(ads_sens);
}

void Widget::setting_fov(int fov)
{
    lb_before_fov->setNum(fov);
}

void Widget::set_cbb1(int index)
{
    cbb_1->setCurrentIndex(index);
}

void Widget::set_cbb2(int index)
{
    cbb_2->setCurrentIndex(index);
}

void Widget::set_cbb3(int index)
{
    cbb_3->setCurrentIndex(index);
}

void Widget::set_cbb4(int index)
{
    cbb_4->setCurrentIndex(index);
}

void Widget::set_cbb5(int index)
{
    cbb_5->setCurrentIndex(index);
}

void Widget::set_cbb6(int index)
{
    cbb_6->setCurrentIndex(index);
}

void Widget::setsetting()
{
    if(!le_sens->text().isEmpty())
    {
        if(le_sens->text().toDouble()>=0&&le_sens->text().toDouble()<=1)
        {
            emit set_sens(le_sens->text().toDouble());
        }
    }
    if(!le_ads_sens->text().isEmpty())
    {
        if(le_ads_sens->text().toDouble()>=0&&le_ads_sens->text().toDouble()<=1)
        {
            emit set_ads_sens(le_ads_sens->text().toDouble());
        }
    }
    if(!le_fov->text().isEmpty())
    {
        if(le_fov->text().toInt()>=70&&le_fov->text().toInt()<=90)
        {
            emit set_fov(le_fov->text().toInt());
        }
    }
    emit getsetting();
}

void Widget::setguns()
{
    emit setcbb(cbb_1->currentIndex(),cbb_2->currentIndex(),cbb_3->currentIndex(),cbb_4->currentIndex(),cbb_5->currentIndex(),cbb_6->currentIndex());
    emit getguns();
}

void Widget::setgun(int index)
{
    switch(index)
    {
    case 0:
        gunname->setText(QStringLiteral("无"));
        gunpic->setText(QStringLiteral("无"));
        break;
    case 1:
        gunname->setText(cbb_1->currentText());
        Lua::getinstance()->setcurrentgunname(cbb_1->currentText());
        gunpic->setPixmap(QPixmap(":/pic/guns/"+cbb_1->currentText()+".png"));
        emit getscopeinfo(cbb_1->currentText());
        break;
    case 2:
        gunname->setText(cbb_2->currentText());
        Lua::getinstance()->setcurrentgunname(cbb_2->currentText());
        gunpic->setPixmap(QPixmap(":/pic/guns/"+cbb_2->currentText()+".png"));
        emit getscopeinfo(cbb_2->currentText());
        break;
    case 3:
        gunname->setText(cbb_3->currentText());
        Lua::getinstance()->setcurrentgunname(cbb_3->currentText());
        gunpic->setPixmap(QPixmap(":/pic/guns/"+cbb_3->currentText()+".png"));
        emit getscopeinfo(cbb_3->currentText());
        break;
    case 4:
        gunname->setText(cbb_4->currentText());
        Lua::getinstance()->setcurrentgunname(cbb_4->currentText());
        gunpic->setPixmap(QPixmap(":/pic/guns/"+cbb_4->currentText()+".png"));
        emit getscopeinfo(cbb_4->currentText());
        break;
    case 5:
        gunname->setText(cbb_5->currentText());
        Lua::getinstance()->setcurrentgunname(cbb_5->currentText());
        gunpic->setPixmap(QPixmap(":/pic/guns/"+cbb_5->currentText()+".png"));
        emit getscopeinfo(cbb_5->currentText());
        break;
    case 6:
        gunname->setText(cbb_6->currentText());
        Lua::getinstance()->setcurrentgunname(cbb_6->currentText());
        gunpic->setPixmap(QPixmap(":/pic/guns/"+cbb_6->currentText()+".png"));
        emit getscopeinfo(cbb_6->currentText());
        break;
    default:
        break;
    }
}

void Widget::setscope(QString scope)
{
    if(scope==QStringLiteral("机瞄"))
    {
        scopename->setText(QStringLiteral("机瞄"));
        scopepic->setText(QStringLiteral("无"));
        return;
    }

    scopename->setText(scope);
    scopepic->setPixmap(QPixmap(":/pic/scope/"+scope));
}
